package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.BusinessGroup;
import com.qian.api.ddj.service.IBusinessGroupService;
import com.qian.service.ddj.dao.BusinessGroupRepository;
import com.qian.service.ddj.dao.BusinessRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjBusinessGroup;
import com.qian.service.ddj.entity.DdjChannel;
import com.yaowk.api.common.BaseException;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class BusinessGroupServiceImpl extends BaseServiceImpl<BusinessGroup, DdjBusinessGroup, Integer> implements IBusinessGroupService {

    @Autowired
    private BusinessGroupRepository dao;
    @Autowired
    private BusinessRepository businessDao;
    @Autowired
    private UserRepository userDao;

    protected BaseRepository<DdjBusinessGroup, Integer> getDao() {
        return dao;
    }

    protected DdjBusinessGroup model2Entity(BusinessGroup model) {
        DdjBusinessGroup ddjBusinessGroup = null;
        if (isNotEmpty(model)) {
            ddjBusinessGroup = new DdjBusinessGroup();
            ddjBusinessGroup.setAllowAliCode(model.getAllowAliCode());
            ddjBusinessGroup.setAllowAliWap(model.getAllowAliWap());
            ddjBusinessGroup.setAllowJd(model.getAllowJd());
            ddjBusinessGroup.setAllowQq(model.getAllowQq());
            ddjBusinessGroup.setAllowQuickBank(model.getAllowQuickBank());
            ddjBusinessGroup.setAllowWxCode(model.getAllowWxCode());
            ddjBusinessGroup.setAllowWxH5(model.getAllowWxH5());
            ddjBusinessGroup.setAllowQqWap(model.getAllowQqWap());
            ddjBusinessGroup.setTariffQqWap(model.getTariffQqWap());
            ddjBusinessGroup.setAllowCodeBank(model.getAllowCodeBank());
            ddjBusinessGroup.setTariffCodeBank(model.getTariffCodeBank());
            ddjBusinessGroup.setBackup1(model.getBackup1());
            ddjBusinessGroup.setBackup2(model.getBackup2());
            ddjBusinessGroup.setBackup3(model.getBackup3());
            ddjBusinessGroup.setBackup4(model.getBackup4());
            ddjBusinessGroup.setBackup5(model.getBackup5());
            ddjBusinessGroup.setCashFixed(model.getCashFixed());
            ddjBusinessGroup.setCashPercent(model.getCashPercent());
            DdjChannel channel = new DdjChannel();
            channel.setId(model.getChannelId());
            ddjBusinessGroup.setChannel(channel);
            ddjBusinessGroup.setId(model.getId());
            ddjBusinessGroup.setMinCashFee(model.getMinCashFee());
            ddjBusinessGroup.setName(model.getName());
            ddjBusinessGroup.setD0(model.getD0());
            ddjBusinessGroup.setD1(model.getD1());
            ddjBusinessGroup.setT0(model.getT0());
            ddjBusinessGroup.setT1(model.getT1());
            ddjBusinessGroup.setLazyTime(model.getLazyTime());
            ddjBusinessGroup.setSettlementTime(model.getSettlementTime());
            ddjBusinessGroup.setTariffAliCode(model.getTariffAliCode());
            ddjBusinessGroup.setTariffAliWap(model.getTariffAliWap());
            ddjBusinessGroup.setTariffJd(model.getTariffJd());
            ddjBusinessGroup.setTariffQq(model.getTariffQq());
            ddjBusinessGroup.setTariffQuickBank(model.getTariffQuickBank());
            ddjBusinessGroup.setTariffWxCode(model.getTariffWxCode());
            ddjBusinessGroup.setTariffWxH5(model.getTariffWxH5());
            ddjBusinessGroup.setMinCashMoney(model.getMinCashMoney());
        }
        return ddjBusinessGroup;
    }

    protected BusinessGroup entity2model(DdjBusinessGroup entity) {
        BusinessGroup businessGroup = null;
        if (isNotEmpty(entity)) {
            businessGroup = new BusinessGroup();
            businessGroup.setAllowAliCode(entity.getAllowAliCode());
            businessGroup.setAllowAliWap(entity.getAllowAliWap());
            businessGroup.setAllowJd(entity.getAllowJd());
            businessGroup.setAllowQq(entity.getAllowQq());
            businessGroup.setAllowQuickBank(entity.getAllowQuickBank());
            businessGroup.setAllowWxCode(entity.getAllowWxCode());
            businessGroup.setAllowWxH5(entity.getAllowWxH5());
            businessGroup.setBackup1(entity.getBackup1());
            businessGroup.setBackup2(entity.getBackup2());
            businessGroup.setBackup3(entity.getBackup3());
            businessGroup.setBackup4(entity.getBackup4());
            businessGroup.setBackup5(entity.getBackup5());
            businessGroup.setCashFixed(entity.getCashFixed());
            businessGroup.setCashPercent(entity.getCashPercent());
            businessGroup.setChannelId(entity.getChannelId());
            businessGroup.setId(entity.getId());
            businessGroup.setMinCashFee(entity.getMinCashFee());
            businessGroup.setName(entity.getName());
            businessGroup.setD0(entity.getD0());
            businessGroup.setD1(entity.getD1());
            businessGroup.setT0(entity.getT0());
            businessGroup.setT1(entity.getT1());
            businessGroup.setLazyTime(entity.getLazyTime());
            businessGroup.setSettlementTime(entity.getSettlementTime());
            businessGroup.setTariffAliCode(entity.getTariffAliCode());
            businessGroup.setTariffAliWap(entity.getTariffAliWap());
            businessGroup.setTariffJd(entity.getTariffJd());
            businessGroup.setTariffQq(entity.getTariffQq());
            businessGroup.setAllowQqWap(entity.getAllowQqWap());
            businessGroup.setTariffQqWap(entity.getTariffQqWap());
            businessGroup.setAllowCodeBank(entity.getAllowCodeBank());
            businessGroup.setTariffCodeBank(entity.getTariffCodeBank());
            businessGroup.setTariffQuickBank(entity.getTariffQuickBank());
            businessGroup.setTariffWxCode(entity.getTariffWxCode());
            businessGroup.setTariffWxH5(entity.getTariffWxH5());
            businessGroup.setMinCashMoney(entity.getMinCashMoney());
        }
        return businessGroup;
    }

    @Override
    public BusinessGroup save(BusinessGroup model) {

        BigDecimal percent = new BigDecimal(0);
        BigDecimal h = new BigDecimal(100);

        if (model.isNew()) {
            percent = percent.add(model.getD0()).add(model.getD1()).add(model.getT0()).add(model.getT1());
            if (percent.compareTo(h) != 0) {
                throw new BaseException("结算金额不足100%");
            }
            model = simpleSave(model);
        } else {
            DdjBusinessGroup ddjBusinessGroup = dao.findOne(model.getId());
            if (isNotEmpty(model.getAllowAliCode()))
                ddjBusinessGroup.setAllowAliCode(model.getAllowAliCode());
            if (isNotEmpty(model.getAllowAliWap()))
                ddjBusinessGroup.setAllowAliWap(model.getAllowAliWap());
            if (isNotEmpty(model.getAllowJd()))
                ddjBusinessGroup.setAllowJd(model.getAllowJd());
            if (isNotEmpty(model.getAllowQq()))
                ddjBusinessGroup.setAllowQq(model.getAllowQq());
            if (isNotEmpty(model.getAllowQuickBank()))
                ddjBusinessGroup.setAllowQuickBank(model.getAllowQuickBank());
            if (isNotEmpty(model.getAllowWxCode()))
                ddjBusinessGroup.setAllowWxCode(model.getAllowWxCode());
            if (isNotEmpty(model.getAllowWxH5()))
                ddjBusinessGroup.setAllowWxH5(model.getAllowWxH5());
            if (isNotEmpty(model.getBackup1()))
                ddjBusinessGroup.setBackup1(model.getBackup1());
            if (isNotEmpty(model.getBackup2()))
                ddjBusinessGroup.setBackup2(model.getBackup2());
            if (isNotEmpty(model.getBackup3()))
                ddjBusinessGroup.setBackup3(model.getBackup3());
            if (isNotEmpty(model.getBackup4()))
                ddjBusinessGroup.setBackup4(model.getBackup4());
            if (isNotEmpty(model.getBackup5()))
                ddjBusinessGroup.setBackup5(model.getBackup5());
            if (isNotEmpty(model.getCashFixed()))
                ddjBusinessGroup.setCashFixed(model.getCashFixed());
            if (isNotEmpty(model.getCashPercent()))
                ddjBusinessGroup.setCashPercent(model.getCashPercent());
            if (isNotEmpty(model.getMinCashFee()))
                ddjBusinessGroup.setMinCashFee(model.getMinCashFee());
            if (isNotEmpty(model.getD0()))
                ddjBusinessGroup.setD0(model.getD0());
            if (isNotEmpty(model.getD1()))
                ddjBusinessGroup.setD1(model.getD1());
            if (isNotEmpty(model.getT0()))
                ddjBusinessGroup.setT0(model.getT0());
            if (isNotEmpty(model.getT1()))
                ddjBusinessGroup.setT1(model.getT1());
            if (isNotEmpty(model.getLazyTime()))
                ddjBusinessGroup.setLazyTime(model.getLazyTime());
            if (isNotEmpty(model.getSettlementTime()))
                ddjBusinessGroup.setSettlementTime(model.getSettlementTime());
            if (isNotEmpty(model.getName()))
                ddjBusinessGroup.setName(model.getName());
            if (isNotEmpty(model.getTariffAliCode()))
                ddjBusinessGroup.setTariffAliCode(model.getTariffAliCode());
            if (isNotEmpty(model.getTariffAliWap()))
                ddjBusinessGroup.setTariffAliWap(model.getTariffAliWap());
            if (isNotEmpty(model.getTariffJd()))
                ddjBusinessGroup.setTariffJd(model.getTariffJd());
            if (isNotEmpty(model.getTariffQq()))
                ddjBusinessGroup.setTariffQq(model.getTariffQq());
            if (isNotEmpty(model.getAllowQqWap()))
                ddjBusinessGroup.setAllowQqWap(model.getAllowQqWap());
            if (isNotEmpty(model.getTariffQqWap()))
                ddjBusinessGroup.setTariffQqWap(model.getTariffQqWap());
            if (isNotEmpty(model.getAllowCodeBank()))
                ddjBusinessGroup.setAllowCodeBank(model.getAllowCodeBank());
            if (isNotEmpty(model.getTariffCodeBank()))
                ddjBusinessGroup.setTariffCodeBank(model.getTariffCodeBank());
            if (isNotEmpty(model.getTariffQuickBank()))
                ddjBusinessGroup.setTariffQuickBank(model.getTariffQuickBank());
            if (isNotEmpty(model.getTariffWxCode()))
                ddjBusinessGroup.setTariffWxCode(model.getTariffWxCode());
            if (isNotEmpty(model.getTariffWxH5()))
                ddjBusinessGroup.setTariffWxH5(model.getTariffWxH5());
            if (isNotEmpty(model.getMinCashMoney()))
                ddjBusinessGroup.setMinCashMoney(model.getMinCashMoney());
            percent = percent.add(ddjBusinessGroup.getD0()).add(ddjBusinessGroup.getD1()).add(ddjBusinessGroup.getT0()).add(ddjBusinessGroup.getT1());
            if (percent.compareTo(h) != 0) {
                throw new BaseException("结算金额不足100%");
            }
            model = entity2model(dao.save(ddjBusinessGroup));
        }
        return model;
    }

    @Override
    public BusinessGroup findByBusinessId(Integer businessId) {
        return entity2model(businessDao.findOne(businessId).getBusinessGroup());
    }
}
